/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Venta;
import persistence.VentaDAO;
import persistence.VentaPersistFactory;

/**
 *
 * @author Alex
 */
public class ventaServlet extends MyCoolServlet {

    @Override
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        VentaDAO persistenceManager = VentaPersistFactory.getVentaDAO(persistenceMechanism);
        Venta venta = generateVentaFromRequest(request);

        if (venta != null && persistenceManager.newVenta(venta)) {
            request.setAttribute("product", venta);
            request.setAttribute("message", "ha sido creado con éxito");
            gotoURL(successForm, request, response);
        } else {
            gotoURL(errorForm, request, response);
        }
    }

    Venta generateVentaFromRequest (HttpServletRequest request) throws NumberFormatException {
    
        Venta venta = new Venta();
        String name = request.getParameter("nombrecliente");
        String direccion = request.getParameter("direccion");
        String formaPago = request.getParameter("formapago");
        

        if (true) {

            //validaciones
        }
        
        venta.setNombreCliente(name);
        venta.setDireccion(direccion);
        venta.setFormaPago(formaPago);
        venta.setIdentificador(venta.generarIdentificador(venta));
        
        return venta;
    }

    private boolean validateName(String name) {
        String namePattern = "[A-Z]([a-zA-Z������������ ])*";
        return name.matches(namePattern);
    }

   


   
}
